#include <QtGui>
#include "mainwindow.h"
#include "userconfig.h"




MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
	menuBar()->addMenu(tr("&File"));
	menuBar()->addMenu(tr("&Edit"));
	menuBar()->addMenu(tr("&View"));
	menuBar()->addMenu(tr("&Settings"));
	menuBar()->addMenu(tr("&Help"));
	statusBar()->showMessage(tr("Thanks Giving The World"));



	CreateStrategyDock();

	CreateInstDock();

	CreateStrategysTab();

	
	setWindowTitle(tr("TF"));


	m_timer = NULL;

	AppInit();
}

MainWindow::~MainWindow()
{

}



void MainWindow::CreateStrategyDock()
{
	
	m_instDock = new QDockWidget();
	//m_strategyDock->setObjectName("dockWidget_objectname");
	///*dockWidget->setWidget(dockSplitter);*/
	addDockWidget(Qt::LeftDockWidgetArea, m_instDock);

}

void MainWindow::CreateInstDock()
{
	//m_bariDock = new BarinfoDock("BarInfo");

	//addDockWidget(Qt::LeftDockWidgetArea, m_bariDock);


	m_strategyDock = new QDockWidget();

	addDockWidget(Qt::LeftDockWidgetArea, m_strategyDock);
}

void MainWindow::CreateStrategysTab()
{
	m_pStrategyTabs = new QTabWidget;
	m_pStrategyTabs->setTabPosition(QTabWidget::South);

	m_qtstra1 = new QtStra1(m_instDock, m_strategyDock);
	m_qtstra2 = new QtStra2(m_instDock, m_strategyDock);
	m_qtstra3 = new QtStra3(m_instDock, m_strategyDock);


	m_pStrategyTabs->addTab(m_qtstra1, tr("Strategy1"));
	m_pStrategyTabs->addTab(m_qtstra2, tr("Strategy2"));
	m_pStrategyTabs->addTab(m_qtstra3, tr("Strategy3"));
	//m_pStrategyTabs->addTab(new QtStra1(m_instDock, m_strategyDock), tr("Strategy3"));

	setCentralWidget(m_pStrategyTabs);

	connect(m_pStrategyTabs, SIGNAL(currentChanged(int)), this, SLOT(TabChanged(int)));


	//force a slot invoke
	m_pStrategyTabs->setCurrentIndex(0);
	m_pStrategyTabs->setCurrentIndex(1);
}


//update bardock info
void	MainWindow::TabChanged (int index)
{
	if (0 == index)
	{
		QtStra1* pcurForm = static_cast<QtStra1*>(m_pStrategyTabs->widget(index));


		pcurForm->OnUpdateInstDockAndStrategyDock();
	}
	else if (1 == index)
	{
		QtStra2* pcurForm = static_cast<QtStra2*>(m_pStrategyTabs->widget(index));


		pcurForm->OnUpdateInstDockAndStrategyDock();
	}
	else if (2 == index)
	{
		QtStra3* pcurForm = static_cast<QtStra3*>(m_pStrategyTabs->widget(index));


		pcurForm->OnUpdateInstDockAndStrategyDock();
	}
	
}

void MainWindow::AppInit()
{

	/*********************Init Global Part*************************************/


	//1. init td api spi
	//2. login in 
	//3. confirm settle info
	if (false == CStrategySys::Login_TradingDay_InstRead(gBroker, gUser, gPassword, gtraderFront, 0x00000111))
	{
		pantheios::log_ERROR("[AppInit]Init Initptd failed");
		return;

	}

	/************************************************************************/


	/*********************Init Every Strategy And Init Bar From Old Init*********************************/

	{
		m_qtstra1->CreateThread();
		m_qtstra2->CreateThread();
		m_qtstra3->CreateThread();
		//m_mfcstra3.CreateThread();
		//m_mfcstra4.CreateThread();
	}

	{
		//Init bars from old init, if first int doesn't work;
		m_qtstra1->OnReinitAllBars();
		m_qtstra2->OnReinitAllBars();
		m_qtstra3->OnReinitAllBars();
	}


	/************************************************************************/




	/*********************Init Timer*********************************/


	if (NULL == m_timer)//init only once
	{
		#if (0 == RUN_STATE || 1 == RUN_STATE)

			m_timer = new QtMainTimer(1000 * 60 * 1, this);

		#else

			m_timer = new QtMainTimer(1000 * 60 * 10, this);

		#endif

			connect(m_timer, SIGNAL(timeout()), this, SLOT(ProcessTimer()));

			m_timer->Start();
	}



	/************************************************************************/

}

void MainWindow::AppDestroy()
{


	//-------------------1. destroy exist bars and every strategy-------------------------------------------------------


	m_qtstra1->OnDestroyAllBars();
	m_qtstra2->OnDestroyAllBars();
	m_qtstra3->OnDestroyAllBars();


	m_qtstra1->DestroyThread();
	m_qtstra2->DestroyThread();
	m_qtstra3->DestroyThread();



	//-------------------------------------------------------------------------------------------------

	//-------------------2. destroy ptd-------------------------------------------------------

	if (false == CStrategySys::Logout())
	{
		pantheios::log_ERROR("[AppDestroy] Destroyptd failed");
	}

	//-------------------------------------------------------------------------------------------------


}

void MainWindow::ProcessTimer()
{

#if 0
	if (m_timer->TimerFire(TimerFire_namespace::statisfiretimer))
	{
		//AppReleaseFrameTimer(TimerFire_namespace::statisfiretimer);
		m_qtstra1->OnTimer(TimerFire_namespace::statisfiretimer);
		m_qtstra2->OnTimer(TimerFire_namespace::statisfiretimer);
		m_qtstra3->OnTimer(TimerFire_namespace::statisfiretimer);
	}



#endif

#if 1
	if (m_timer->TimerFire(TimerFire_namespace::appreinitfiretimer))
	{

		MainWindow* parent = static_cast<MainWindow*>(m_timer->parent());
		parent->setEnabled(false);



		SYSTEMTIME tm;
		GetLocalTime(&tm);
		pantheios::log_NOTICE("[TimerRoutine] start(", panint(tm.wYear), "_", panint(tm.wMonth), "_", panint(tm.wDay), "_", panint(tm.wHour), ":", panint(tm.wMinute), ":", panint(tm.wSecond), "), Ready AppTimer");



		CTimeCounter tc;
		tc.Start();

		

		/*********************Destroy*********************************/

		{
			AppDestroy();
		}


		/************************************************************************/



		/*********************Reinit*********************************/

		{
			AppInit();

		}

		/************************************************************************/

		
		GetLocalTime(&tm);
		pantheios::log_NOTICE("[TimerRoutine] end(", panint(tm.wYear), "_", panint(tm.wMonth), "_", panint(tm.wDay), "_", panint(tm.wHour), ":", panint(tm.wMinute), ":", panint(tm.wSecond), "), elapse:", panreal(tc.Elapse()), ", Finish AppTimer");


		parent->setEnabled(true);
	}


#endif	




}